package com.han.jdbc.mybatis.spring;
/**
* @author hanpeng
* @version 创建时间：2020年7月20日 上午11:55:50
* @ClassName 类名称
* @Description 类描述
*/

import java.sql.SQLException;

import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.druid.pool.DruidDataSource;
import com.han.jdbc.mybatis.bean.Emp;
import com.han.jdbc.mybatis.spring.mappers.Apple;
import com.han.jdbc.mybatis.spring.mappers.EmpService;
import com.han.jdbc.mybatis.spring.mappers.EmpSpringMpper;
import com.han.jdbc.mybatis.spring.mappers.EmpSpringMpperSpringDi;


public class TestSpringMybatis {
	@Test
	public void getApplicationContext() throws SQLException {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		DruidDataSource ds = context.getBean("dataSource", DruidDataSource.class);
		SqlSessionFactory ssf = context.getBean("sqlSessionFactory", SqlSessionFactory.class);
		System.out.println(ssf);
		ds.close();
	}
	@Test
	public void getMapperByAnnotation() throws SQLException {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		EmpSpringMpper mpper = context.getBean("empSpringMpper", EmpSpringMpper.class);
		Emp emp = mpper.getEmpByEId(1);
		System.out.println(emp);
	}
	@Test
	public void getMapperConfig() throws SQLException {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		EmpSpringMpper mpper = context.getBean("empSpringMpper", EmpSpringMpper.class);
		Emp emp = mpper.getEmpByEId(1);
		System.out.println(emp);
	}
	@Test
	public void getMapperScan() throws SQLException {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		EmpSpringMpperSpringDi mpper = context.getBean( EmpSpringMpperSpringDi.class);
		Emp emp = mpper.getEmpByEId(1);
		System.out.println(emp);
	}
	@Test
	public void getTransaction() throws SQLException {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		EmpService service = context.getBean( EmpService.class);
		try {
			service.addEmp(1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
